<template>
  <!-- 布局容器 -->
  <el-container class="home-container">
    <!-- 头部容器 -->
    <el-header>
      <div>
        <!--<img src="../assets/cat.jpg" alt />-->
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏容器 -->
      <el-aside :width="isCollpase?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="isCollpase?'el-icon-s-unfold':'el-icon-s-fold'"></i>
        </div>
        <!-- 侧边栏菜单区组件 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          :unique-opened="true"
          :collapse="isCollpase"
          :collapse-transition="false"
          :router="true"
          :default-active="$route.path"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <!-- 二级菜单模板区 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
        <!-- 底部容器 -->
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      iconsObj: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      // 是否折叠菜单标志
      isCollpase: false
    };
  },
  // 生命周期函数
  created() {
    this.getMenuList();
  },
  methods: {
    logout() {
      // 清空token
      window.sessionStorage.clear();
      // 路由重定向到登录页
      this.$router.push("/login");
    },
    // 获取所有菜单
    getMenuList() {
      this.$http.get("menus").then(response => {
        // console.log(response);
        var result = response.data;
        if (result.meta.status !== 200)
          return this.$message.error(result.meta.msg);
        this.menuList = result.data;
      });
    },
    // 点击按钮折叠菜单
    toggleCollapse() {
      this.isCollpase = !this.isCollpase;
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.el-footer {
  background-color: #373d41;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  text-align: center;
}
</style>